using System.Linq;
using System.Web.Mvc;
using HomePortal.Models;

namespace HomePortal.Controllers
{

    public class TagsController : Controller
    {
        private readonly ITagRepository tagRepository;

        // If you are using Dependency Injection, you can delete the following constructor
        public TagsController()
            : this(new TagRepository())
        {
        }

        public TagsController(ITagRepository tagRepository)
        {
            this.tagRepository = tagRepository;
        }

        //
        // GET: /Tags/

        [Authorize(Users = "admin")]
        public ViewResult Index()
        {
            return View(tagRepository.AllIncluding(tag => tag.Posts));
        }

        //
        // GET: /Tags/Details/5
        [Authorize(Users = "admin")]
        public ViewResult Details(int id)
        {
            return View(tagRepository.Find(id));
        }

        //
        // GET: /Tags/Create
        [Authorize(Users = "admin")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Tags/Create
        [Authorize(Users = "admin")]
        [HttpPost]
        public ActionResult Create(Tag tag)
        {
            if (ModelState.IsValid)
            {
                tagRepository.InsertOrUpdate(tag);
                tagRepository.Save();
                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /Tags/Edit/5
        [Authorize(Users = "admin")]
        public ActionResult Edit(int id)
        {
            return View(tagRepository.Find(id));
        }

        //
        // POST: /Tags/Edit/5
        [Authorize(Users = "admin")]
        [HttpPost]
        public ActionResult Edit(Tag tag)
        {
            if (ModelState.IsValid)
            {
                tagRepository.InsertOrUpdate(tag);
                tagRepository.Save();
                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /Tags/Delete/5
        [Authorize(Users = "admin")]
        public ActionResult Delete(int id)
        {
            return View(tagRepository.Find(id));
        }

        //
        // POST: /Tags/Delete/5
        [Authorize(Users = "admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            tagRepository.Delete(id);
            tagRepository.Save();

            return RedirectToAction("Index");
        }

        public ActionResult ListTags()
        {
            var tags = tagRepository.All.ToList();
            return View(tags);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                tagRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

